﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Web.UI.WebControls;
using HRM.BLL;
using HRM.DAL.DAO;

namespace HRM.UI.HRManagerUI.PunishmentUI
{
    public partial class ConfigPunishment : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetAllProcedure();
                PopulateType();
            }
        }

        private void GetAllProcedure()
        {
            try
            {
                PunishedProcedureManager manager = new PunishedProcedureManager();
                List<PunishedProcedure> procedures = new List<PunishedProcedure>();
                procedures = manager.GetAllProcedure();
                punishedProcedureGdv.DataSource = procedures;
                punishedProcedureGdv.DataBind();
             
            }
            catch (Exception)
            {
                throw;
            }
        }

        protected void SavePunishedProcedure(object o, EventArgs e)
        {
            try
            {
                PunishedProcedure procedure = new PunishedProcedure();
                PunishedProcedureManager manager = new PunishedProcedureManager();
                procedure.Procedure = txtProcedure.Value;
                procedure.Descreption = txtProcedureDetails.Value;
                if (manager.SaveProcedure(procedure))
                {
                    msgLabel.ForeColor = Color.Green;
                    msgLabel.Text = "Procedure Saved Successfully";
                    txtProcedure.Value ="";
                    txtProcedureDetails.Value ="";
                    GetAllProcedure();
                }
                else
                {
                    msgLabel.ForeColor = Color.Red;
                    msgLabel.Text = "Procedure not Saved";
                }
            }
            catch (Exception ex)
            {
                msgLabel.ForeColor = Color.Red;
                msgLabel.Text = ex.Message;
            }
        }
         protected void SavePunishmentType(object o,EventArgs e)
         {
             try
             {
                 PunishmentType type = new PunishmentType();
                 type.Type = txtPunishmentType.Value;
                 PunishmentTypeManager typeManager = new PunishmentTypeManager();
                 msgLbl.Text = typeManager.SavePunishedType(type);
                 if (msgLbl.Text.Contains("Exist"))
                 {
                     msgLbl.ForeColor = Color.Red;  
                 }
                 else
                 {
                     msgLbl.ForeColor = Color.Green;
                     txtPunishmentType.Value = "";
                 }
                PopulateType();
             }
             catch (Exception ex)
             {
                 msgLbl.Text =ex.Message;
                 msgLabel.ForeColor = Color.Red;
             }
         }

        private void PopulateType()
        {
            List<PunishmentType> punishmentTypes = new List<PunishmentType>();
            PunishmentTypeManager manager = new PunishmentTypeManager();
            punishmentTypes = manager.GetAllPunishmentType();
            punishedTypeGdv.DataSource = punishmentTypes;
            punishedTypeGdv.DataBind();
           
        }

        protected void punishedProcedureGdv_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
           punishedProcedureGdv.PageIndex = e.NewPageIndex;
           GetAllProcedure();
        }

        protected void punishedTypeGdv_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            punishedTypeGdv.PageIndex = e.NewPageIndex;
            PopulateType();
        }

        protected void punishedProcedureGdv_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                PunishedProcedureManager punishedProcedure=new PunishedProcedureManager();
                int id = Convert.ToInt32(((Label)punishedProcedureGdv.Rows[e.RowIndex].FindControl("lblid")).Text);
                string msg = punishedProcedure.DeleteProcedure(id);
                if (msg.Contains("Successfully"))
                {
                    Lblpunishpro.Text = "Punished Procedure Successfully deleted";
                    Lblpunishpro.ForeColor = Color.Green;
                    GetAllProcedure();

                }
                else
                {
                    Lblpunishpro.Text = "sorry !Punished Procedure not deleted";
                    Lblpunishpro.ForeColor = Color.Red;
                }

            }
            catch (Exception)
            {
                Lblpunishpro.Text = "sorry !Punished Procedure not deleted";
                Lblpunishpro.ForeColor = Color.Red;
            }
        }


        protected void punishedTypeGdv_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
               PunishmentTypeManager punishmentType=new PunishmentTypeManager();
                int Id = Convert.ToInt32(((Label)punishedTypeGdv.Rows[e.RowIndex].FindControl("lblId")).Text);
                string msg = punishmentType.DeletePunishType(Id);
                if (msg.Contains("Successfully"))
                {
                    LblPunishType.Text = "Punished Type Successfully deleted";
                    LblPunishType.ForeColor = Color.Green;
                    PopulateType();

                }
                else
                {
                    LblPunishType.Text = "sorry !Punished Type not deleted";
                    LblPunishType.ForeColor = Color.Red;
                }

            }
            catch (Exception)
            {
                LblPunishType.Text = "sorry !Punished Type not deleted";
                LblPunishType.ForeColor = Color.Red;
            }
        }


    }
}